package com.kc.web.controller.business;

import com.kc.business.domain.KcShareDistributionApply;
import com.kc.business.domain.vo.KcShareDistributionApplyVo;
import com.kc.business.service.KcShareDistributionApplyService;
import com.kc.common.annotation.Log;
import com.kc.common.core.controller.BaseController;
import com.kc.common.core.domain.AjaxResult;
import com.kc.common.core.domain.model.LoginUser;
import com.kc.common.core.page.TableDataInfo;
import com.kc.common.enums.BusinessType;
import com.kc.common.utils.ServletUtils;
import com.kc.common.utils.SharePosterUtils;
import com.kc.framework.web.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * FileName: KcShareDistributionApplyController
 * Author:   TP
 * Date:     2020-11-14 09:41
 * Description:
 */
@RestController
@RequestMapping("business/shareDistributionApply")
public class KcShareDistributionApplyController extends BaseController {

    @Autowired
    private KcShareDistributionApplyService shareDistributionApplyService;

    @Autowired
    private TokenService tokenService;
    @Autowired
    private SharePosterUtils sharePosterUtils;

    /**
     * 获取分销申请列表
     */
    @GetMapping("/list")
    @PreAuthorize("@ss.hasPermi('business:shareDistributionApply:list')")
    public TableDataInfo getList(KcShareDistributionApplyVo applyVo) {
        startPage();
        List<KcShareDistributionApplyVo> list = shareDistributionApplyService.getList(applyVo);
        return getDataTable(list);
    }

    /**
     * 获取分销申请详情
     */
    @GetMapping("/info/{id}")
    public AjaxResult getInfo(@PathVariable Integer id) {
        if (id != null && id > 0) {
            return AjaxResult.success(shareDistributionApplyService.getInfoById(id));
        }
        return AjaxResult.error("请指定分销申请记录");
    }

    /**
     * 审核分分销用户，请求参数申请记录主键ID、审核状态
     */
    @Log(title = "审核分销用户", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('business:shareDistributionApply:audit')")
    @PutMapping("/audit")
    public AjaxResult audit(@RequestBody KcShareDistributionApply apply) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        //shareDistributionApplyService.createSharePoster();
        if (loginUser != null) {
            apply.setUpdateBy(loginUser.getUsername());
            apply.setUpdateTime(LocalDateTime.now());
            return shareDistributionApplyService.audit(apply);
        }

        return AjaxResult.error("操作失败，未获取到当前登陆人信息");
    }

}
